/*
 * @Author: 追逐的娃 kangchao_php@163.com
 * @Date: 2024-05-06 10:16:32
 * @LastEditors: 追逐的娃 kangchao_php@163.com
 * @LastEditTime: 2024-05-08 10:08:08
 * @FilePath: \huijie-admin\src\permission.js
 * @Description: 
 * 
 */
import { router,addRoutes } from '@/router'
import {getToken} from '@/utils/auth'
import { useSystemStore } from '@/pinia'

//全局守卫导航
let hasGetInfo = false
router.beforeEach(async (to,from,next) => {
    //添加加载条

    //获取token
    let system = useSystemStore()
    const token  = system.token

    //token不存在并不是登录页面
    if(!token && to.path != '/login'){
        //提醒先登录

        //返回到登录页面
        return next({path:'/login'})
    }
    //token存在防止重复登录
    if(token && to.path == '/login'){
        //提醒请勿登录

        //回跳到首页
        return next({path: from.path ? from.path : '/'})
    }
    //如果用户登陆了，自动获取用户信息，并存储到pinia中
    let hasNewRoutes = false
    if(token && !hasGetInfo){
        const system = useSystemStore()
        //去请求pinia中定义的请求信息接口菜单
        let { menus } = await system.getUserInfo()
        hasGetInfo = true
        //动态添加路由
        hasNewRoutes = addRoutes(menus)
    }

    //设置页面标题
    let title = (to.meta.title ? to.meta.title : '')
    document.title = title
    hasNewRoutes ? next(to.fullPath) : next()
})

//全局后置守卫(页面渲染完成)
router.afterEach((to,from)=>{
    //隐藏滚动条
})